/**
 * Created by leezhenbo on 15/9/9.
 */
package com.ryot.managers {
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.filesystem.File;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;

import mx.collections.ArrayCollection;

public class AppManager extends EventDispatcher{
    public function AppManager() {
        _projects = new ArrayCollection();
    }

    private static var _instance:AppManager;

    private var _projects:ArrayCollection;
    private var _rootPath:String = File.documentsDirectory.nativePath + File.separator + '_RM' + File.separator;
    private var _loader:URLLoader;

    public static function getInstance():AppManager{
        if(_instance == null) _instance = new AppManager();
        return _instance;
    }

    /**
     * 启动
     */
    public function start(callback:Function = null):void{
        _loader = new URLLoader();
        _loader.dataFormat = URLLoaderDataFormat.TEXT;
        _loader.addEventListener(Event.COMPLETE,function():void{
            var xml:XML = new XML(_loader.data);
            for each(var projectXML:XML in xml.projects.project){
                var obj:Object = {};
                obj.name = projectXML.name.toString();
                obj.language = projectXML.language.toString();
                _projects.addItem(obj);
            }
            if(callback) callback();
        });
        _loader.load(new URLRequest("configs/roots.xml"));
    }

    public function get projects():ArrayCollection{
        return this._projects;
    }

    public function get rootPath():String{
        return this._rootPath;
    }
}
}
